<?php
class Api_Lib_Rsp_Upload {

    function __construct() {

    }

    /**
     * @desc ：二维码验证 回写验证状态
     * 手机端上传图片专用接口
     * Time：2019/07/17 14:17:33
     * @author Wuyou
    */
    function callbackImage($params = array(),& $service){
        $params = $params['params'];
        $imagesCache = FLEA::getCache('IMAGES.'.$params['token'] , -1);

        $imageFiles = $params['image'];
        //保存图片到本地
        //获取64位图片信息
        if( preg_match('/^(data:\s*image\/(\w+);base64,)/', $imageFiles, $result) ){
            $type = strtolower($result[2]);
            $type == 'jpeg' && $type = 'jpg';
            $image = base64_decode(str_replace($result[1], '', $imageFiles));
            // dump($result);
            $pathRoot = 'upload/Images/';
            $randName = date('ymdHis') . rand(10000, 99999).'.'.$type;
            $imagePath = $pathRoot.$randName;

            //把图片放入服务器目录中
            $putSize = file_put_contents($imagePath ,$image);

            $info = getimagesize($imageFiles);
            $width = $info[0]+0;
            $height = $info[1]+0;

            //保存到表里
            $arr = array(
                'path'      => $imagePath,
                'smallPath' => '',
                'width'     => $width,
                'height'    => $height,
                'time'      => time(),
            );

            $_model = FLEA::getSingleton('Model_Jichu_Image');
            $id = $_model->save($arr);
            $imgInfo = array(
                'name'    =>$randName,
                'imgPath' =>$imagePath,
                'imageId' =>$id,
                'index'   =>$params['index'],
            );

            //追加到原来的缓存中去
            $imagesCache['images'][] = $imgInfo;
            //重新排序
            $imagesCache['images'] = array_column_sort($imagesCache['images'] ,'index');
            $imagesCache['count'] = $params['count'];
            FLEA::writeCache('IMAGES.'.$params['token'] ,$imagesCache);
        }

        $data = array(
            'success'   => true,
            'msg'       => '请求成功',
            'errorcode' => '0',
        );

        return $data;
    }
}
?>